<template>
  <div class="main">
    <el-card class="box-card offset-md-6">
      <div slot="header" class="clearfix">
        <span>后台登陆</span>
      </div>
      <el-form
        :model="ruleForm2"
        status-icon
        ref="ruleForm2"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="手机号">
          <el-input placeholder="手机号" @keyup.13="submitForm()" v-model="ruleForm2.loginID"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            type="password"
            placeholder="密码"
            @keyup.13="submitForm()"
            auto-complete="off"
            v-model="ruleForm2.password"
          ></el-input>
        </el-form-item>
        <button type="button" class="btn btn-success btn-block" @click="submitForm()">登录</button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
// import io from "socket.io-client";
export default {
  name: "login",
  data() {
    return {
      radio2: 3,
      ruleForm2: {
        loginID: "",
        password: ""
      },
      alls: "class1",
      socket: null
    };
  },
  watch: {
    radio2: function(value) {
      this.alls = value;
    }
  },
  mounted() {
    // this.socket = io("/", {
    //   // 实际使用中可以在这里传递参数
    //   query: {
    //     room: "demo",
    //     userId: `client_${Math.random()}`
    //   },

    //   transports: ["websocket"]
    // });
    // const socket = this.socket;
    // const { log } = console;
    // socket.on("connect", () => {
    //   const id = socket.id;

    //   log("#connect,", id, socket);

    //   // 监听自身 id 以实现 p2p 通讯
    //   socket.on(id, msg => {
    //     log("#receive,", msg);
    //   });
    // });

    // // 接收在线用户信息
    // socket.on("online", msg => {
    //   log("#online,", msg);
    // });

    // // 系统事件
    // socket.on("disconnect", msg => {
    //   log("#disconnect", msg);
    // });

    // socket.on("disconnecting", () => {
    //   log("#disconnecting");
    // });

    // socket.on("error", () => {
    //   log("#error");
    // });
  },
  beforeDestroy() {
    // this.socket.close();
  },
  methods: {
    ...mapActions(["login"]),
    async submitForm() {
      if (!this.ruleForm2.loginID && !this.ruleForm2.password) {
        this.$alert("输入的用户名和密码不能为空", "错误提示", {
          confirmButtonText: "确定"
        });
      } else if (!this.ruleForm2.loginID) {
        this.$alert("输入的用户名不能为空!");
      } else if (!this.ruleForm2.password) {
        this.$alert("输入的密码不能为空!");
      } else {
        try {
          await this.login(this.ruleForm2);
          this.$router.push("/index");
        } catch (err) {
          this.$message({
            message: "" + err + "!",
            type: "warning"
          });
        }
      }
    },
    guabbi() {
      this.dialogFormVisible = false;
    }
  }
};
</script>

<style scoped>
.main {
  background-image: url(https://xd-taixuhuanjing.oss-cn-qingdao.aliyuncs.com/322560.jpg);
  height: 100%;
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
}
.box-card {
  width: 400px;
  background-color: rgba(255, 255, 255, 0.7);
}
.box-card:hover {
  background-color: rgb(255, 255, 255);
}
</style>
